package com.j3l11234.myy_sign.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.j3l11234.myy_sign.entity.UserEntity;
import com.j3l11234.myy_sign.model.AttendModel;
import com.j3l11234.myy_sign.util.PrivilegeCheck;

@Controller
@RequestMapping("/attend")
public class AttendController {
	@Autowired
	private AttendModel attendModel;
	
	@RequestMapping(value="/signin", method=RequestMethod.POST)
	@PrivilegeCheck(privilege = UserEntity.Worker, needLogin = true)
    public Object signin(ModelMap model, HttpSession session) {
		
		UserEntity user = (UserEntity) session.getAttribute("user");
		
		//DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		//DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
		
		Date date = new Date();
        try {
        	attendModel.addAttend(user.getId(), 1, date, date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "redirect:/home/index.do";
    }
	
	@RequestMapping(value="/signout", method=RequestMethod.POST)
	@PrivilegeCheck(privilege = UserEntity.Worker, needLogin = true)
    public Object signout(ModelMap model, HttpSession session) {
		
		UserEntity user = (UserEntity) session.getAttribute("user");
		
		//DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		//DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
		
		Date date = new Date();
        try {
        	attendModel.addAttend(user.getId(), 2, date, date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "redirect:/home/index.do";
    }
	
}
